package org.hackystat.devcathlon.ui.page.teams.invitations;

import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.hackystat.devcathlon.engine.team.TeamManager;
import org.hackystat.devcathlon.engine.user.User;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.teams.TeamsSession;

/**
 * Panel that holds all the selected invitation's details.
 * 
 * @author John Ly
 *
 */
public class InvitationDetailPanel extends Panel {
  /** Support serialization. */
  private static final long serialVersionUID = 1L;

  /** Constructs the invitation detail panel with the wicket name, id. 
   * @param id The wicket id name.
   */
  public InvitationDetailPanel (String id) {
    super(id);
    TeamsSession session = DevcathlonSession.get().getTeamsSession();
    
    String owner = "";
    if (session.getOwner() != null) {
      owner = session.getOwner().getEmail();      
    }
    
    add(new Label("TeamNameLabel", session.getSelectedTeam()));
    add(new Label("ProjectNameLabel", session.getProjectName()));    
    add(new Label("TeamOwnerLabel", owner));
    
    List<User> everyone = session.getMembers();
    everyone.addAll(session.getInvited());
        
    ListView listView = new ListView("MemberList", everyone) {
      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(ListItem item) {
        User user = (User) item.getModelObject();
        item.add(new Label("MemberName", user.getEmail()));        
      }
      
    };
    add(listView);
    
    // Add accept button to form.
    Form form = new Form("InviteForm");
    
    Button accept = new Button("AcceptButton") {
      private static final long serialVersionUID = 1L;
      
      @Override
      public void onSubmit() {
        String teamName = DevcathlonSession.get().getTeamsSession().getSelectedTeam();
        if (!("".equals(teamName))) {
          String email = DevcathlonSession.get().getEmail();
          String password = DevcathlonSession.get().getPassword();
          TeamManager.getInstance().getTeam(teamName).acceptTeamInvite(email, password);
          DevcathlonSession.get().getTeamsSession().clearSelectedTeam();
          setResponsePage(new TeamsInvitationPage());
        }
      }            
    };
    
    // Add cancel button to form.
    Button decline = new Button("DeclineButton") {
      private static final long serialVersionUID = 1L;
      
      @Override
      public void onSubmit() {
        String teamName = DevcathlonSession.get().getTeamsSession().getSelectedTeam();
        if (!("".equals(teamName))) {
          String email = DevcathlonSession.get().getEmail();
          TeamManager.getInstance().getTeam(teamName).declineTeamInvite(email);
          DevcathlonSession.get().getTeamsSession().clearSelectedTeam();
          setResponsePage(new TeamsInvitationPage());
        }
      }      
    };
    
    form.add(accept);
    form.add(decline);
    add(form);
    
  }
  
}
